package com.wontube.smr.web.controller.admin;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.constants.SiteConstants;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.web.client.admin.IAdminGenreHandler;
import com.wontube.smr.web.client.admin.IAdminGenreSiteHandler;
import com.wontube.smr.web.client.model.po.GenreSite;

@Controller
@RequestMapping("/admin/sitegenre")
public class AdminGenreSiteControll {
	private final static String SUCCESS = "Done";
	private final static String FAILURE = "Failure: something wrong";
	
	@RequestMapping("")
	public ModelAndView gotoGenreSiteAdminHome(){
		ModelAndView mv = new ModelAndView("admin/genresite/genresite_index");
		IAdminGenreSiteHandler handler = getAdminGenreSiteHandler();
		List<GenreSite> genreSites = handler.selectAllGenreSites();
		String[] category = Genre.getAllGenreCategories();
		String[] website = SiteConstants.getAllWebsites();
		mv.addObject("genreSites", genreSites);
		mv.addObject("categorys", category);
		mv.addObject("website",website);
		return mv;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public String addGenreSite(GenreSite genreSite){
		IAdminGenreSiteHandler handler = getAdminGenreSiteHandler();
		genreSite.setId(UUID.randomUUID().toString());
		genreSite.setVersion(new Date());
		boolean bl = handler.saveOrUpdateGenreSite(genreSite);
		if(bl){
			return SUCCESS;
		}
		return FAILURE;
	}
	
	@RequestMapping("/get/{id}")
	public ModelAndView getGenreSiteByID(@PathVariable("id") String id){
		ModelAndView mv = new ModelAndView("admin/genresite/update_genresite");
		IAdminGenreSiteHandler handler = getAdminGenreSiteHandler();
		GenreSite genreSite = handler.getGenreSiteByPK(id);
		String[] category = Genre.getAllGenreCategories();
		String[] website = SiteConstants.getAllWebsites();
		mv.addObject("categorys", category);
		mv.addObject("website",website);
		mv.addObject("genreSite", genreSite);
		return mv;
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public String updateGenreSite(GenreSite genreSite){
		genreSite.setVersion(new Date());
		IAdminGenreSiteHandler handler = getAdminGenreSiteHandler();
		boolean success = handler.updateGenreSiteByPK(genreSite);
		if(success){
			return SUCCESS;
		}
		return FAILURE;
	}
	
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public String deleteGenre(@PathVariable("id") String id){
		IAdminGenreSiteHandler handler = getAdminGenreSiteHandler();
		boolean bl = handler.deleteGenreSite(id);
		if(bl){
			return SUCCESS;
		}
		return FAILURE;
	}
	
	@RequestMapping("/getGenres")
	@ResponseBody
	public String getGenres(@RequestParam("category") String category){
		IAdminGenreHandler handler = getAdminGenreHandler();
		List<Genre> genre =  handler.selectGenreByCategory(category);
		return new Gson().toJson(genre);
	}
	
	private IAdminGenreSiteHandler getAdminGenreSiteHandler(){
		Remoting<IAdminGenreSiteHandler> remoting = new Remoting<IAdminGenreSiteHandler>();
		return remoting.createService(IAdminGenreSiteHandler.class);
	}
	
	private IAdminGenreHandler getAdminGenreHandler(){
		Remoting<IAdminGenreHandler> remoting = new Remoting<IAdminGenreHandler>();
		return remoting.createService(IAdminGenreHandler.class);
	}
}
